require 'time'
require 'basic.rb'
require 'higher.rb'

class Movie
	include Comparable # Modules 2
	include Enumerable # Higher-order functions 2
	
	attr_reader :name
	attr_reader :showtime
	def initialize(name,showtime)
		@name = name
		if showtime.is_a?(Time) then
			@showtime = showtime
		elsif showtime.is_a?(String) then
			@showtime = Time.parse(showtime)
		else
			raise ArgumentError
		end
	end

	def name_and_showtime
		@name + ": " + ((Time.now < @showtime) ? "next show" : "already started") + " at " + @showtime.strftime('%I:%M %p')
	end

	# Modules 1
	def <=>(movie2)
		if movie2.is_a?(Movie) then
			@showtime <=> movie2.showtime
		end
	end
end

movies = []
movies << Movie.new("Orphan", Time.parse("1:00 pm"))
movies << Movie.new("Orphan", Time.parse("4:05 pm"))
movies << Movie.new("Orphan", Time.parse("7:00 pm"))
movies << Movie.new("GI Joe", Time.parse("1:05 pm"))
movies << Movie.new("GI Joe", Time.parse("4:30 pm"))
movies << Movie.new("GI Joe", Time.parse("6:50 pm"))
movies << Movie.new("Rocky Horror", Time.parse("12:00 am"))
movies << Movie.new("District 9", Time.parse("11:00 am"))
movies << Movie.new("District 9", Time.parse("3:00 pm"))

movies[1].between?(movies[0],movies[2]) # Modules 2
movies.sort() # Modules 3

movies.group_by { |movie| movie.name } # Higher-order functions 2
movies.select { |movie| movie.showtime > Time.parse("5:00pm")} # Higher-order functions 3.1
movies.count {|movie| movie.showtime > Time.parse("5:00pm") } # Higher-order functions 3.2
((movies.select { |movie| movie.showtime > Time.parse("5:00pm")}).collect {|movie| movie.name}).uniq # Higher-order functions 3.3
